package by.bsu.fami.greyscalecodefrontend.utils;

import static java.lang.Math.round;
import android.graphics.Bitmap;
import android.graphics.Color;

public final class GreyscaleMaker {
	public static byte[][] scaleAndMakeGrey(Bitmap bitmap, float scaleFactor){
		return makeGrey(scaleBitmap(bitmap, scaleFactor));
	}
	
	public static Bitmap scaleBitmap(Bitmap bitmap, float scaleFactor){
		if (scaleFactor == 1.0f) {
			return bitmap;
		}
		int newH = round(scaleFactor * bitmap.getHeight());
		int newW = round(scaleFactor * bitmap.getWidth());
		return Bitmap.createScaledBitmap(bitmap, newW, newH, true);
	}
	
	public static byte[][] makeGrey(Bitmap bitmap) {
		int h = bitmap.getHeight();
		int w = bitmap.getWidth();
		int color = 0;
		byte image[][] = new byte[h][w];
		for (int x = 0; x < w; x++) {
			for (int y = 0; y < h; y++) {
				color = bitmap.getPixel(x, y);
				image[y][x] = (byte) ((Color.red(color) * 11 + 
						Color.green(color) * 16 + 
						Color.blue(color) * 5) / 32);
			}
		}
		return image;
	}
}
